﻿using OpenNETCF.IoC;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace IoTCM
{
    internal partial class RequestProcessor
    {
        private void ProcessGet(HttpListenerContext context)
        {
            Debug.WriteLine("GET: " + context.Request.Url.ToString());

            var host = GetRequestedHost(context);
            if (host == null)
            {
                return;
            }

            // first check the session
            if (!ValidateSession(context, host))
            {
                return;
            }

            if (context.Request.Url.Segments.Length < 2)
            {
                SendNotFoundError(context, host);
                return;
            }

            var entityName = context.Request.Url.Segments[1].ToLower().Replace("/", string.Empty);

            switch (context.Request.Url.Segments.Length)
            {
                case 2:
                    // request for "all" of an entity
                    switch (entityName)
                    {
                        case "things":
                            var things = host.GetSerializedThings();
                            SendResponse(context, things, host.MimeType);
                            break;
                        case "values":
                            var values = host.GetSerializedValues();
                            SendResponse(context, values, host.MimeType);
                            break;
                        default:
                            SendNotFoundError(context, host);
                            break;
                    }

                    break;
                case 3:
                    // request for specific entity
                    // get the requested ID
                    var id = context.Request.Url.Segments[2];

                    switch (entityName)
                    {
                        case "things":
                            var things = host.GetSerializedThing(id);

                            if (things == null)
                            {
                                SendNotFoundError(context, host);
                                return;
                            }

                            SendResponse(context, things, host.MimeType);
                            break;
                        case "values":
                            var values = host.GetSerializedValues(id);

                            if (values == null)
                            {
                                SendNotFoundError(context, host);
                                return;
                            }

                            SendResponse(context, values, host.MimeType);
                            break;
                        default:
                            SendNotFoundError(context, host);
                            break;
                    }
                    break;
                default:
                    SendNotFoundError(context, host);
                    break;
            }
        }
    }
}
